package com.jianguozh.difs.framework.model;

import cn.hutool.core.util.URLUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpUtil;
import com.jianguozh.difs.framework.constant.enums.LogTypeEnum;
import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 操作日志
 *
 * @author hanrs
 * @email 4076130@qq.com
 * @since 2022/6/9 17:43
 */
@Getter
@Setter
public class OperateLog {

    public OperateLog() {
        HttpServletRequest request = ((ServletRequestAttributes) Objects
                .requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        this.type = LogTypeEnum.NORMAL;
        this.remoteAddr = ServletUtil.getClientIP(request);
        this.requestUri = URLUtil.getPath(request.getRequestURI());
        this.method = request.getMethod();
        this.userAgent = request.getHeader(HttpHeaders.USER_AGENT);
        this.params = HttpUtil.toParams(request.getParameterMap());
    }

    /**
     * 操作者
     */
    private String operator;

    /**
     * 操作时间
     */
    private LocalDateTime operateDate;

    /**
     * 日志类型
     */
    private LogTypeEnum type;

    /**
     * 日志标题
     */
    private String title;

    /**
     * 操作ip地址
     */
    private String remoteAddr;

    /**
     * 浏览器类型
     */
    private String userAgent;

    /**
     * 请求uri
     */
    private String requestUri;

    /**
     * 操作方式
     */
    private String method;

    /**
     * 请求参数
     */
    private String params;

    /**
     * 耗时
     */
    private Long cost;

    /**
     * 异常
     */
    private String exception;

    /**
     * 应用标识
     */
    private String appId;
}
